package com.probiz.estore.sales.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.dao.DataIntegrityViolationException;
import org.apache.commons.lang.RandomStringUtils;
import com.probiz.estore.common.util.DateUtil;
import com.probiz.estore.sales.SalesConstants;


public class GenerateCodeUtil
{

    private static Random random = new Random(new Date().getTime());
    
    public static String generateCouponNo(int style, String prefix)
    {
        StringBuffer result = new StringBuffer();

        if (prefix != null)
        {
            result = result.append(prefix);
        }
        if (style == SalesConstants.COUPON_NO_STYLE_NUMBER)
        {
            while (result.length() < 16)
            {
                result = result.append(random.nextInt(10));
            }
        }
        else if (style == SalesConstants.COUPON_NO_STYLE_STRING)
        {

            while (result.length() < 16)
            {
                int ascii = random.nextInt(26) + 97;
                //ascii += 97;
                char aaa = (char) ascii;
                result = result.append(aaa);
            }
        }
        return result.toString();
    }
    
    public static void main(String[] args){
    	//产生5500条随机不相同礼券号码
    	List<String> list = new ArrayList<String>();
    	for(int i=0; i<5500; i++){
    		String no = GenerateCodeUtil.generateGiftCertificateNo();
    		int j = 0;
    		while (list.contains(no)) {
    			j++;
    			if (j > SalesConstants.MAX_GEN_TRY_TIMES) {
    				throw new DataIntegrityViolationException(
    				"GiftCertificateNo is repeat.");
    			}
    			no = GenerateCodeUtil.generateGiftCertificateNo();
    		}
    		list.add(no);
    	}
    	for(String str: list){
    		System.out.println(str);
    	}
    }

    public static String generateGiftCertificateNo()
    {
        StringBuffer result = new StringBuffer();
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append("-");
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append("-");
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append("-");
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        result = result.append(random.nextInt(10));
        return result.toString();
    }
    
	/**
	 * 生成编码 (编码业务类型标识) (年-两位数字) (月-两位数字)(日-两位数字)(随机数字位)
	 * 
	 */
	public static String generateCode(String Tag, int count ){
		return Tag + DateUtil.fmtTodayToSix()+RandomStringUtils.random(count,false,true);
	}
}
